/*************************************************************************
> File Name: tcp_client.c
> Author: hzy
> Created Time: 2023-09-20 16:29:59
> Description:
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char const *argv[])
{
    // 创建套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        perror("socket");
        return -1;
    }

    // 设置服务器信息
    struct sockaddr_in server_addr;
    bzero(&server_addr, sizeof(server_addr)); // 初始化服务器地址
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(10086);
    inet_pton(AF_INET, "10.7.178.214", &server_addr.sin_addr);

    // 连接服务器
    int connect_ret = connect(sockfd,
                              (struct sockaddr *)&server_addr,
                              sizeof(server_addr));
    if (connect_ret != 0)
    {
        perror("connect");
        return -1;
    }

    // 与服务器通信
    while (1)
    {
        char send_buf[512] = "";
        printf("发什么消息\n");
        fgets(send_buf, sizeof(send_buf), stdin);
        // 向服务器发送消息
        send(sockfd, send_buf, strlen(send_buf), 0);
    }

    // 关闭套接字
    close(sockfd);

    return 0;
}